package com.jianan.mis.app.overseastudy;

import java.awt.Dimension;
import java.awt.Toolkit;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.jianan.mis.data.domain.OsConsultation;
import com.jianan.mis.data.domain.OsConsultationState;
import com.jianan.mis.data.domain.Session;
import com.jianan.mis.service.OsConsultationService;
import com.jianan.mis.sys.IGroupKeys;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.SpringBeanFactory;

public class OsConsultationDialog extends Dialog {

	private OsConsultationComposite consultationComposite;
	
	private OsConsultation osConsultation;
	
	private boolean readonly;
	
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public OsConsultationDialog(Shell parentShell) {
		super(parentShell);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("留学咨询表");
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {

		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		composite.setLayout(gridLayout);
	
		final Label left_label = new Label(composite, SWT.NONE);
		final GridData gd_label = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		left_label.setLayoutData(gd_label);

		final ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.V_SCROLL | SWT.H_SCROLL);
		scrolledComposite.setAlwaysShowScrollBars(false);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height - 150 > 585 ? 585 : screenSize.height - 150;
		scrolledComposite.setLayoutData(new GridData(SWT.DEFAULT, height));

		consultationComposite = new OsConsultationComposite(scrolledComposite, SWT.NONE);
		consultationComposite.setMode(1);
		consultationComposite.setSize(consultationComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		consultationComposite.setOsConsultation(osConsultation);
		if (readonly) {
			consultationComposite.setEnabled(false);
		} else {
			consultationComposite.setEnabled(true);
		}
		scrolledComposite.setContent(consultationComposite);

		final Label right_label = new Label(composite, SWT.NONE);
		final GridData gd_label_1 = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		right_label.setLayoutData(gd_label_1);
		
		//
		return composite;
	}
	
	public void setOsConsultation(OsConsultation osConsultation) {
		this.osConsultation = osConsultation;
	}
	
	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		if (readonly) {
			createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.OK_LABEL, true);
		} else {
			createButton(parent, IDialogConstants.OK_ID, "保存", true);
			if ((Session.getInstance().getGroups().contains(IGroupKeys.OSCHIEF)
					|| Session.getInstance().getGroups().contains(IGroupKeys.MANAGER))
					&& !osConsultation.getState().equals(OsConsultationState.AUDITING)) {
				createButton(parent, IDialogConstants.YES_ID, "签约审核", false);
			}
			createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		}
	}

	protected void okPressed() {
		super.okPressed();
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			try {
				// 更新留学咨询表
				OsConsultation updatedConsultation = consultationComposite
						.getOsConsultation();
				if (updatedConsultation == null) {
					return;
				}
				if (MessageDialog.openConfirm(getShell(),
						IMessageKeys.CONFIRM, "确认更新该留学咨询表信息吗?")) {
					OsConsultationService osConsultationService = (OsConsultationService) SpringBeanFactory
							.getBean("osConsultationService");
					updatedConsultation.setState(osConsultation.getState());
					osConsultationService.update(updatedConsultation);
					MessageDialog.openInformation(getShell(),
							IMessageKeys.SUCCESS, "档案号为"
									+ updatedConsultation.getId()
									+ "的留学咨询表保存成功!");
				} else {
					return;
				}
			} catch (Exception ex) {
				MessageDialog.openError(getShell(), IMessageKeys.FAIL, ex
						.getMessage());
			}
		} else if(buttonId == IDialogConstants.YES_ID) {
			try {
				// 签约审核
				OsConsultation updatedConsultation = consultationComposite
						.getOsConsultation();
				if (updatedConsultation == null) {
					return;
				}
				if (MessageDialog.openConfirm(getShell(),
						IMessageKeys.CONFIRM, "确定签约审核该留学咨询表信息吗?")) {
					OsConsultationService osConsultationService = (OsConsultationService) SpringBeanFactory
							.getBean("osConsultationService");
					updatedConsultation.setState(OsConsultationState.AUDITING);
					osConsultationService.update(updatedConsultation);
					MessageDialog.openInformation(getShell(),
							IMessageKeys.SUCCESS, "档案号为"
									+ updatedConsultation.getId()
									+ "的留学签约审核成功!");
					buttonId = IDialogConstants.OK_ID;
				} else {
					return;
				}
			} catch (Exception ex) {
				MessageDialog.openError(getShell(), IMessageKeys.FAIL, ex
						.getMessage());
			}
		}
		super.buttonPressed(buttonId);
	}
	
	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(740, 690);
	}

}
